using System;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Reflection;
using System.Globalization;
using System.Windows.Forms;

namespace Camalon.Drawing
{
	/// <summary>
	/// Summary description for SizeFConverter.
	/// </summary>
	public class AnchorPointConverter:ExpandableInstanceConverter
	{
		public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
		{
			if ( value is string ) 
			{
				string sVal = (string)value;
				if( sVal.Length == 0 ) return AnchorPoint.Default;
				if( string.Compare( sVal, "Empty",true, culture) == 0) return AnchorPoint.Empty;
				if( string.Compare( sVal, "Default",true, culture) == 0) return AnchorPoint.Default;
				string[] args = sVal.Split(',');

				if( args.Length < 2 ) throw base.GetConvertFromException(value);


				int x = int.Parse( args[0], null);
				int y = int.Parse( args[1], null);

				

				AnchorStyles anchor = AnchorStyles.None;
				if( args.Length >=2 ) anchor|= (AnchorStyles)Enum.Parse( typeof( AnchorStyles ), args[2], true);
				if( args.Length >=3 ) anchor|= (AnchorStyles)Enum.Parse( typeof( AnchorStyles ), args[3], true);
				if( args.Length >=4 ) anchor|= (AnchorStyles)Enum.Parse( typeof( AnchorStyles ), args[4], true);
				if( args.Length >=5 ) anchor|= (AnchorStyles)Enum.Parse( typeof( AnchorStyles ), args[5], true);
				
				return new AnchorPoint( x,y, anchor);
			}


			return base.ConvertFrom (context, culture, value);
		}

		public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
		{

			if( value is AnchorPoint )
			{
				AnchorPoint m = (AnchorPoint) value;

				if (destinationType == typeof(string)) 
				{
					return String.Format( "{0}, {1}, {2}", m.X,m.Y,m.Anchor);
				}

				if (destinationType == typeof(InstanceDescriptor) ) 
				{

					if( m.Anchor == (AnchorStyles.Top|AnchorStyles.Left))//Default
					{
						ConstructorInfo ctor = typeof(AnchorPoint).GetConstructor(new Type[] {typeof(int), typeof(int)});
						if (ctor != null) return new InstanceDescriptor(ctor, new object[] {m.X,m.Y});
					}
					else
					{
						ConstructorInfo ctor = typeof(AnchorPoint).GetConstructor(new Type[] {typeof(int), typeof(int), typeof(AnchorStyles)});
						if (ctor != null) return new InstanceDescriptor(ctor, new object[] {m.X,m.Y, m.Anchor});
					}
				}
			}

			return base.ConvertTo (context, culture, value, destinationType);
		}

	}


	#region AnchorPointConverterF Commented
//	public class AnchorPointConverterF:ExpandableInstanceConverter
//	{
//		private int _precision = 0;
//
//		public AnchorPointConverterF():this(0)
//		{
//		}
//
//		public AnchorPointConverterF( int precision ):base()
//		{
//			if( precision<0) precision = 0; 
//			_precision = precision;
//		}
//
//		public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
//		{
//			if ( value is string ) 
//			{
//				FileWatcher.WriteLine( (string)value);
//
//				string sVal = (string)value;
//				if( sVal.Length == 0 ) return AnchorPoint.Default;
//				if( string.Compare( sVal, "Empty",true, culture) == 0) return AnchorPoint.Empty;
//				if( string.Compare( sVal, "Default",true, culture) == 0) return AnchorPoint.Default;
//				string[] args = sVal.Split(',');
//
//				if( args.Length < 2 ) throw base.GetConvertFromException(value);
//
//
//				float x = float.Parse( args[0], null);
//				float y = float.Parse( args[1], null);
//
//				x = (float)System.Math.Round( x,_precision );
//				y = (float)System.Math.Round( y,_precision );
//				
//
//				AnchorStyles anchor = AnchorStyles.None;
//				if( args.Length >=2 ) anchor|= (AnchorStyles)Enum.Parse( typeof( AnchorStyles ), args[2], true);
//				if( args.Length >=3 ) anchor|= (AnchorStyles)Enum.Parse( typeof( AnchorStyles ), args[3], true);
//				if( args.Length >=4 ) anchor|= (AnchorStyles)Enum.Parse( typeof( AnchorStyles ), args[4], true);
//				if( args.Length >=5 ) anchor|= (AnchorStyles)Enum.Parse( typeof( AnchorStyles ), args[5], true);
//				
//				FileWatcher.WriteLine( anchor.ToString());
//
//				return new AnchorPoint( x,y, anchor);
//			}
//
//
//			return base.ConvertFrom (context, culture, value);
//		}
//
//		public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
//		{
//
//			if( value is AnchorPoint )
//			{
//				AnchorPoint m = (AnchorPoint) value;
//				m.X = (float)System.Math.Round( m.X,_precision );
//				m.Y = (float)System.Math.Round( m.Y,_precision );
//
//				if (destinationType == typeof(string)) 
//				{
//					return String.Format( "{0}, {1}, {2}", m.X,m.Y,m.Anchor);
//				}
//
//				if (destinationType == typeof(InstanceDescriptor) ) 
//				{
//
//					if( m.Anchor == (AnchorStyles.Top|AnchorStyles.Left))//Default
//					{
//						ConstructorInfo ctor = typeof(AnchorPoint).GetConstructor(new Type[] {typeof(float), typeof(float)});
//						if (ctor != null) return new InstanceDescriptor(ctor, new object[] {m.X,m.Y});
//					}
//					else
//					{
//						ConstructorInfo ctor = typeof(AnchorPoint).GetConstructor(new Type[] {typeof(float), typeof(float), typeof(AnchorStyles)});
//						if (ctor != null) return new InstanceDescriptor(ctor, new object[] {m.X,m.Y, m.Anchor});
//					}
//				}
//			}
//
//			return base.ConvertTo (context, culture, value, destinationType);
//		}
//
//	}
//
	#endregion AnchorPointConverterF

}
